# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('ruby' 'ruby-docs')
pkgver=2.1.5
pkgrel=1
arch=('i686' 'x86_64')
url='http://www.ruby-lang.org/en/'
license=('BSD' 'custom')
makedepends=('openssl-devel' 'libffi-devel' 'doxygen' 'libcrypt-devel'  'gmp-devel' 'libyaml-devel' 'libgdbm-devel' 'libiconv-devel' 'libreadline-devel' 'zlib-devel')
options=('!emptydirs' '!makeflags' 'staticlibs')
source=("http://cache.ruby-lang.org/pub/ruby/${pkgver%.*}/ruby-${pkgver//_/-}.tar.bz2"
        1.8.7-cygwin-tcltk.patch
        2.0.0-cygwin-configure.patch
        2.0.0-cygwin-rubygems.patch
        2.0.0-pkgconfig-version.patch
        'gemrc'
        'ruby-2.1.2-msys2.patch')
md5sums=('a7c3e5fec47eff23091b566e9e1dac1b'
         '1d525a071cb78d8008f77324a5b50ab3'
         '27732924ae632c64ccaa17e11d27f452'
         'ae640ceb3bcc952a0d71db0f1aea434c'
         '11bd0e9bc8fd66ef7d19676e22cda7de'
         '6fb8e7a09955e0f64be3158fb4a27e7a'
         '0bfc8522577be17423b0beb6d6a9e981')

prepare() {
  cd ${srcdir}/ruby-${pkgver//_/-}
  patch -p2 -i ${srcdir}/1.8.7-cygwin-tcltk.patch
  patch -p2 -i ${srcdir}/2.0.0-cygwin-configure.patch
  patch -p2 -i ${srcdir}/2.0.0-cygwin-rubygems.patch
  patch -p2 -i ${srcdir}/2.0.0-pkgconfig-version.patch
  patch -p1 -i ${srcdir}/ruby-2.1.2-msys2.patch
  autoreconf -fi
}

build() {
  mkdir -p ${srcdir}/build-${CHOST} && cd ${srcdir}/build-${CHOST}

  CFLAGS+=" -fno-strict-aliasing "
  CXXFLAGS+=" -fno-strict-aliasing "
  ../ruby-${pkgver//_/-}/configure \
    --build=${CHOST} \
    --prefix=/usr \
    --exec_prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --sharedstatedir=/var/lib \
    --libexecdir=/usr/lib/ruby \
    --enable-shared \
    --disable-rpath \
    --with-dbm-type=gdbm_compat \
    LDSHARED="gcc -shared" \
    DLDFLAGS="-Wl,--export-all-symbols"

  make
}

check() {
  cd ruby-${pkgver//_/-}

  make test
}

package_ruby() {
  pkgdesc='An object-oriented language for quick and easy programming'
  depends=('gcc-libs' 'libopenssl' 'libffi' 'libcrypt'  'gmp' 'libyaml' 'libgdbm' 'libiconv' 'libreadline' 'zlib')
  optdepends=(#'tk: for Ruby/TK'
              'ruby-docs: Ruby documentation')
  provides=('rubygems' 'rake')
  conflicts=('rake')
  backup=('etc/gemrc')
  install='ruby.install'

  cd ${srcdir}/build-${CHOST}

  make DESTDIR="${pkgdir}" install-nodoc

  install -D -m644 ${srcdir}/gemrc "${pkgdir}/etc/gemrc"

  install -D -m644 ${srcdir}/ruby-${pkgver//_/-}/COPYING "${pkgdir}/usr/share/licenses/ruby/LICENSE"
  install -D -m644 ${srcdir}/ruby-${pkgver//_/-}/BSDL "${pkgdir}/usr/share/licenses/ruby/BSDL"
}

package_ruby-docs() {
  pkgdesc='Documentation files for ruby'

  cd ${srcdir}/build-${CHOST}

  make DESTDIR="${pkgdir}" install-doc install-capi

  install -D -m644 ${srcdir}/ruby-${pkgver//_/-}/COPYING "${pkgdir}/usr/share/licenses/ruby-docs/LICENSE"
  install -D -m644 ${srcdir}/ruby-${pkgver//_/-}/BSDL "${pkgdir}/usr/share/licenses/ruby-docs/BSDL"
}
